﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Collections;
using TMHuy.config;

namespace TMHuy
{
    class Travian42
    {
        CookieContainer cookie = new CookieContainer();
        PostSubmitter post;
        public bool debug = true;

        public string server = "http://finals.travian.com/";

        public string username1, password1, screen;
        public Random rand = new Random();
        public int initTime = 1000;
        public int randomRange = 2000;
        public Travian42()
        {
            post = new PostSubmitter(cookie);
        }

        public VillageShortInfo[] login()
        {
            try
            {
                post = new PostSubmitter(cookie);
                String result = post.request(server);
                Thread.Sleep(initTime + rand.Next(randomRange));
                if (debug)
                {
                    Util.writeStringToFile(result, "login.html");
                }
                Regex re = new Regex("<input type=\"hidden\" name=\"login\" value=\"(\\d+)\" />");
                MatchCollection matches = re.Matches(result);
                string login = matches[0].Groups[1].Value;

                post = new PostSubmitter(cookie);
                post.Url = server + "dorf1.php";
                post.PostItems.Add("name", username1);
                post.PostItems.Add("password", password1);
                post.PostItems.Add("s1", "Login");
                post.PostItems.Add("login", login);
                post.PostItems.Add("w", screen);
                post.Type = PostSubmitter.PostTypeEnum.Post;
                result = post.PostWithRef(server);
                Thread.Sleep(initTime + rand.Next(randomRange));
                if (debug)
                {
                    Util.writeStringToFile(result, "afterlogin.html");
                }

                re = new Regex("<img src=\"img/x\\.gif\" class=\"nation nation(\\d+)\" alt=\"(.+?)\" title=\"(.+?)\" />");
                matches = re.Matches(result);
                Constants.tribe = Convert.ToInt32(matches[0].Groups[1].Value) - 1;
                Console.WriteLine(Constants.tribe);



                re = new Regex("<a  href=\"\\?newdid=(\\d+)\" accesskey=\".+?\" class=\".+?\">\\s*<img src=\"img/x\\.gif\" alt=\"\" />\\s*<div class=\"name\">(.+?)</div>\\s*<span class=\".+?\"><span class=\".+?\"><span class=\"coordinateX\">\\((-?\\d+)</span><span class=\"coordinatePipe\">\\|</span><span class=\"coordinateY\">(-?\\d+)\\)");
                matches = re.Matches(result);
                VillageShortInfo[] villages = new VillageShortInfo[matches.Count];
                Console.WriteLine(matches.Count);
                for (int i = 0; i < matches.Count; i++)
                {
                    VillageShortInfo village = new VillageShortInfo();
                    village.id = matches[i].Groups[1].Value;
                    village.name = matches[i].Groups[2].Value;
                    village.x = Convert.ToInt32(matches[i].Groups[3].Value);
                    village.y = Convert.ToInt32(matches[i].Groups[4].Value);
                    villages[i] = village;
                }
                if (villages.Length > 0)
                {
                    return villages;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public void logout()
        {
            post = new PostSubmitter(cookie);
            String result = post.request(server+"logout.php");
        }

        public string request(string url)
        {
            post = new PostSubmitter(cookie);
            return post.request(url);
        }

        public HeroInfo getHeroInfo()
        {
            try
            {
                post = new PostSubmitter(cookie);
                string result = post.request(server+"hero_inventory.php");
                Thread.Sleep(initTime + rand.Next(randomRange));
                if (debug)
                {
                    Util.writeStringToFile(result, "hero.html");
                }
                Regex re = new Regex("<div class=\"element current powervalue\"><span class=\"value\">(\\d+)</span>%</div>");
                MatchCollection matches = re.Matches(result);
                int h = Convert.ToInt32(matches[0].Groups[1].Value);
                re = new Regex("<tr id=\"attributepower\" class=\"attribute power\">\\s*<td class=\"element attribName tooltip\" title=\".+?\">.+?</td>\\s*<td class=\"element current powervalue tooltip\" title=\".+?\">\\s*<span class=\"value\">(\\d+)</span>");
                matches = re.Matches(result);
                int f = Convert.ToInt32(matches[0].Groups[1].Value);
                bool a = result.Contains("isInVillage: true") && result.Contains("isDead: false");
                re = new Regex("culturePoints: (\\d+)");
                matches = re.Matches(result);
                int cp = Convert.ToInt32(matches[0].Groups[1].Value);
                
                re = new Regex("<span id=\"availablePoints\">(\\d+)</span>");
                matches = re.Matches(result);
                int p = 0;
                if (matches.Count > 0)
                {
                   p = Convert.ToInt32(matches[0].Groups[1].Value);
                }

                HeroInfo info = new HeroInfo(h, f, a, 0);
                info.pointsToAdd = p;
                info.equippedItems = new ArrayList();
                info.unequippedItems = new ArrayList();
                re = new Regex("{							id:	(\\d+),							typeId:	(\\d+),							name:	'(.+?)',							placeId:	(\\d+),							place:	'(.+?)',							slot:	'(.+?)',							amount:	(\\d+),                            instant: (.+?),                            isUsableIfDead: (.+?),							attributes: \\[\"(.+?)\"\\]						}");
                matches = re.Matches(result);
                for (int i = 0; i < matches.Count; i++)
                {
                    Item item = new Item();
                    item.id = matches[i].Groups[1].Value;
                    item.typeId = matches[i].Groups[2].Value;
                    item.name = matches[i].Groups[3].Value;
                    item.placeId = Convert.ToInt32(matches[i].Groups[4].Value);
                    item.place = matches[i].Groups[5].Value;
                    item.slot = matches[i].Groups[6].Value;
                    item.ammount = Convert.ToInt32(matches[i].Groups[7].Value);
                    if (item.placeId == 0)
                    {
                        info.equippedItems.Add(item);
                    }
                    else
                    {
                        info.unequippedItems.Add(item);
                    }
                }

                re = new Regex("a: (\\d+),			c: '(.+?)'");
                matches = re.Matches(result);
                info.a = Convert.ToInt32(matches[0].Groups[1].Value);
                info.c = matches[0].Groups[2].Value;
                info.cp = cp;
                return info;
            }
            catch (Exception ex)
            {
                return new HeroInfo(0, 0, false, 0);
            }
        }

        public string postRequestMapInfo(string x, string y)
        {
            Console.WriteLine(server);
            post = new PostSubmitter(cookie);
            post.Url = server+"ajax.php?cmd=mapPositionData";
            post.PostItems.Add("cmd", "mapPositionData");
            post.PostItems.Add("data[x]", x);
            post.PostItems.Add("data[y]", y);
            post.PostItems.Add("data[zoomLevel]", "3");
            post.Type = PostSubmitter.PostTypeEnum.Post;
            string result = post.Post();
            if (debug)
            {
                Util.writeStringToFile(result, "mapdetail.txt");
            }
            return result;
        }

        public bool has(int x, int y, String animal)
        {
            post = new PostSubmitter(cookie);
            post.Url = server+"ajax.php?cmd=viewTileDetails";
            post.PostItems.Add("cmd", "viewTileDetails");
            post.PostItems.Add("x", x.ToString());
            post.PostItems.Add("y", y.ToString());
            post.Type = PostSubmitter.PostTypeEnum.Post;
            string result = post.Post();
            if (debug)
            {
                Util.writeStringToFile(result, "oasisdetail.txt");
            }
            return result.Contains(animal);
        }

        public int getPop(int x, int y)
        {
            post = new PostSubmitter(cookie);
            post.Url = server + "ajax.php?cmd=viewTileDetails";
            post.PostItems.Add("cmd", "viewTileDetails");
            post.PostItems.Add("x", x.ToString());
            post.PostItems.Add("y", y.ToString());
            post.Type = PostSubmitter.PostTypeEnum.Post;
            string result = post.Post();
            if (debug)
            {
                Util.writeStringToFile(result, "oasisdetail.txt");
            }
            Regex re = new Regex("<td>(\\d+)<\\\\/td>");
            MatchCollection matches = re.Matches(result);
            if (matches.Count > 0)
            {
                return Convert.ToInt32(matches[0].Groups[1].Value);
            }
            else
            {
                return 0;
            }
        }

        //not fixed
        public bool raidable(int x, int y)
        {
            post = new PostSubmitter(cookie);
            post.Url = server + "ajax.php?cmd=viewTileDetails";
            post.PostItems.Add("cmd", "viewTileDetails");
            post.PostItems.Add("x", x.ToString());
            post.PostItems.Add("y", y.ToString());
            post.Type = PostSubmitter.PostTypeEnum.Post;
            string result = post.Post();
            if (debug)
            {
                Util.writeStringToFile(result, "oasisdetail.txt");
            }
            if (result.Contains("Send troops") || result.Contains("Raid"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        
        //not fixed
        public String attack(int x, int y, int type, int number, String cityId, bool useHero, int spy)
        {
            String result = post.request(server+"build.php?id=39&tt=2");
            Thread.Sleep(initTime + rand.Next(randomRange));
            if (debug)
            {
                Util.writeStringToFile(result, "sendtroop1.html");
            }
                Regex re = new Regex("<input type=\"hidden\" name=\"timestamp\" value=\"(\\d+)\" />");
                MatchCollection matches = re.Matches(result);
                String timestamp = matches[0].Groups[1].Value;
                re = new Regex("<input type=\"hidden\" name=\"timestamp_checksum\"		value=\"(.+?)\" />");
                matches = re.Matches(result);
                String timestamp_checksum = matches[0].Groups[1].Value;
                re = new Regex("<input type=\"hidden\" name=\"b\"		value=\"(.+?)\" />");
                matches = re.Matches(result);
                String b = matches[0].Groups[1].Value;
                post = new PostSubmitter(cookie);
                post.Url = server+"build.php?id=39&tt=2";
                post.PostItems.Add("timestamp", timestamp);
                post.PostItems.Add("timestamp_checksum", timestamp_checksum);
                post.PostItems.Add("b", b);
                post.PostItems.Add("c", "4");
                post.PostItems.Add("dname", "");
                post.PostItems.Add("s1", "ok");
                post.PostItems.Add("t" + type.ToString(), number.ToString());
                if (useHero)
                {
                    post.PostItems.Add("t11", "1");
                }
                post.PostItems.Add("x", x.ToString());
                post.PostItems.Add("y", y.ToString());
                post.Type = PostSubmitter.PostTypeEnum.Post;
                result = post.Post();
                Thread.Sleep(initTime + rand.Next(randomRange));
                if (debug)
                {
                    Util.writeStringToFile(result, "sendtroop2.html");
                }
                re = new Regex("name=\"timestamp\"\\svalue=\"(\\d+)\" />");
                matches = re.Matches(result);
                timestamp = matches[0].Groups[1].Value;
                re = new Regex("value=\"(.+?)\" /> <input type=\"hidden\" name=\"id\"");
                matches = re.Matches(result);
                timestamp_checksum = matches[0].Groups[1].Value;

                re = new Regex("name=\"id\"\\svalue=\"(\\d+)\" />");
                matches = re.Matches(result);
                String id = matches[0].Groups[1].Value;

                re = new Regex("name=\"a\"\\svalue=\"(\\d+)\" />");
                matches = re.Matches(result);
                String a = matches[0].Groups[1].Value;

                re = new Regex("name=\"kid\"\\svalue=\"(\\d+)\" />");
                matches = re.Matches(result);
                String kid = matches[0].Groups[1].Value;

                post = new PostSubmitter(cookie);
                post.Url = server+"build.php?id=39&tt=2";
                post.PostItems.Add("timestamp", timestamp);
                post.PostItems.Add("timestamp_checksum", timestamp_checksum);
                post.PostItems.Add("a", a);
                post.PostItems.Add("c", "4");
                post.PostItems.Add("id", id);
                post.PostItems.Add("kid", kid);
                post.PostItems.Add("s1", "ok");
                for (int i = 1; i <= 10; i++)
                {
                    if (i == type)
                    {
                        post.PostItems.Add("t" + i.ToString(), number.ToString());
                    }
                    else
                    {
                        post.PostItems.Add("t" + i.ToString(), "0");
                    }
                }
                if (useHero)
                {
                    post.PostItems.Add("t11", "1");
                }
                else
                {
                    post.PostItems.Add("t11", "0");
                }
                if (spy > 0)
                {
                    post.PostItems.Add("spy", spy.ToString());
                }
                post.Type = PostSubmitter.PostTypeEnum.Post;
                result = post.Post();
                Thread.Sleep(initTime + rand.Next(randomRange));
                if (debug)
                {
                    Util.writeStringToFile(result, "sendtroop3.html");
                }

                if (result == "build.php?id=39&tt=1") return "Troop sent at " + DateTime.Now.ToShortTimeString();
            else
            {
                return "Error";
            }
        }

        public bool scoutable(int x, int y)
        {
            try
            {
                post = new PostSubmitter(cookie);
                post.Url = server + "ajax.php?cmd=viewTileDetails";
                post.PostItems.Add("cmd", "viewTileDetails");
                post.PostItems.Add("x", x.ToString());
                post.PostItems.Add("y", y.ToString());
                post.Type = PostSubmitter.PostTypeEnum.Post;
                string result = post.Post();
                result = result.Replace("\\n", "").Replace("\\t", "");
                if (debug)
                {
                    Util.writeStringToFile(result, "oasisdetail.html");
                }
                if (result.Contains("<img src=\\\"img\\/x.gif\\\" class=\\\"iReport iReport16\\\" alt=\\\"\\\" title=\\\"\\\" \\/>") || result.Contains("<img src=\\\"img\\/x.gif\\\" class=\\\"iReport iReport17\\\" alt=\\\"\\\" title=\\\"\\\" \\/>"))
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public int getOasisDefend(int x, int y, bool infantry)
        {
            try
            {
                post = new PostSubmitter(cookie);
                post.Url = server+"ajax.php?cmd=viewTileDetails";
                post.PostItems.Add("cmd", "viewTileDetails");
                post.PostItems.Add("x", x.ToString());
                post.PostItems.Add("y", y.ToString());
                post.Type = PostSubmitter.PostTypeEnum.Post;
                string result = post.Post();
                result = result.Replace("\\n", "").Replace("\\t", "");
                if (debug)
                {
                    Util.writeStringToFile(result, "oasisdetail.txt");
                }
                if (result.Contains("spieler.php")) //Occupied oasis or village
                {
                    if (result.Contains("<table cellpadding=\\\"1\\\" cellspacing=\\\"1\\\" id=\\\"troop_info\\\" class=\\\"rep transparent\\\"><tbody><tr><td><img src=\\\"img\\/x.gif\\\" class=\\\"iReport iReport3\\\" alt=\\\"\\\" title=\\\"\\\" \\/>") || result.Contains("<table cellpadding=\\\"1\\\" cellspacing=\\\"1\\\" id=\\\"troop_info\\\" class=\\\"rep transparent\\\"><tbody><tr><td><img src=\\\"img\\/x.gif\\\" class=\\\"iReport iReport2\\\" alt=\\\"\\\" title=\\\"\\\" \\/>"))
                    {
                        return int.MaxValue;
                    }
                    else
                    {
                        return 0;
                    }
                }
                else
                {
                    Regex re = new Regex("<img class=\\\\\"unit u(\\d+)\\\\\" src=\\\\\"img\\\\/x\\.gif\\\\\" alt=\\\\\".+?\\\\\" title=\\\\\".+?\\\\\" \\\\/><\\\\/td><td class=\\\\\"val\\\\\">(\\d+)<\\\\/td>");
                    MatchCollection matches = re.Matches(result);
                    int infDef = 0;
                    for (int i = 0; i < matches.Count; i++)
                    {
                        int troopType = Convert.ToInt32(matches[0].Groups[1].Value) - 31;
                        int troopNumber = Convert.ToInt32(matches[i].Groups[2].Value);
                        infDef += troopNumber * (infantry ? Util.infDef[troopType] : Util.calDef[troopType]);
                    }
                    return infDef;
                }
            }
            catch (Exception ex)
            {
                return int.MaxValue;
            }
        }


        public bool adventure()
        {
            post = new PostSubmitter(cookie);
            string result = post.request(server+"hero_adventure.php");
            Thread.Sleep(initTime + rand.Next(randomRange));
            if (debug)
            {
                Util.writeStringToFile(result, "adventure.html");
            }
            Regex re = new Regex("<a class=\"gotoAdventure arrow\"  href=\"(.+?)\">.+?</a>");
            MatchCollection matches = re.Matches(result);
            if (matches.Count == 0) return false;
            post = new PostSubmitter(cookie);
            String url = server+ matches[0].Groups[1].Value.Replace("&amp;","&");
            Console.WriteLine(url);

            result = post.request(url);
            Thread.Sleep(initTime + rand.Next(randomRange));
            if (debug)
            {
                Util.writeStringToFile(result, "adventure1.html");
            }
            re = new Regex("<input type=\"hidden\" name=\"a\" value=\"(.+?)\" />");
            matches = re.Matches(result);
            string a = matches[0].Groups[1].Value;
            re = new Regex("<input type=\"hidden\" name=\"kid\" value=\"(.+?)\" />");
            matches = re.Matches(result);
            String kid = matches[0].Groups[1].Value;

            post = new PostSubmitter(cookie);
            post.Url = server+"start_adventure.php";
            post.PostItems.Add("a", a);
            post.PostItems.Add("from", "list");
            post.PostItems.Add("kid", kid);
            post.PostItems.Add("send", "1");
            post.PostItems.Add("start", "Start adventure.");
            post.Type = PostSubmitter.PostTypeEnum.Post;
            result = post.Post();
            Thread.Sleep(initTime + rand.Next(randomRange));
            if (debug)
            {
                Util.writeStringToFile(result, "adventure2.html");
            }
            return true;
        }

        //not fixed
        public bool addToFarmList(string link, int x, int y, int t, int n)
        {
            try
            {
                post = new PostSubmitter(cookie);
                string result = post.request(link);
                if (debug)
                {
                    Util.writeStringToFile(result, "farmlist1.html");
                }
                Regex re = new Regex("<input type=\"hidden\" name=\"a\" value=\"(.+?)\" />");
                MatchCollection matches = re.Matches(result);
                string a = matches[0].Groups[1].Value;
                re = new Regex("<option value=\"(\\d+)\" selected>");
                matches = re.Matches(result);
                string lid = matches[0].Groups[1].Value;
                post = new PostSubmitter(cookie);
                post.Url = server+"build.php?gid=16&tt=99";
                post.PostItems.Add("a", a);
                post.PostItems.Add("action", "addSlot");
                post.PostItems.Add("direction", "asc");
                post.PostItems.Add("lid", lid);
                post.PostItems.Add("save", "save");
                post.PostItems.Add("sort", "distance");
                post.PostItems.Add("lid", lid);
                for (int i = 1; i <= 10; i++)
                {
                    if (i == t)
                    {
                        post.PostItems.Add("t" + i, n.ToString());
                    }
                    else
                    {
                        post.PostItems.Add("t" + i, "0");
                    }
                }
                post.PostItems.Add("target_id", "0");
                post.PostItems.Add("x", x.ToString());
                post.PostItems.Add("y", y.ToString());
                post.Type = PostSubmitter.PostTypeEnum.Post;
                result = post.Post();
                if (debug)
                {
                    Util.writeStringToFile(result, "farmlist2.html");
                }
                if (result.Contains("The account has been banned temporarily"))
                {
                    Console.WriteLine("abc");
                    return false;
                }
                else return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                return false;
            }
        }

        //not fixed
        public string getFarmlistTarget(string villageID, string troopname)
        {
            post = new PostSubmitter(cookie);
            string result = post.request(server + "build.php?gid=16&tt=99");
            Thread.Sleep(initTime + rand.Next(randomRange));
            if (debug)
            {
                Util.writeStringToFile(result, "activatefarmlist1.html");
            }
            return result;
        }

        //not fixed
        public int activateFarmlist(string lid, string a, ArrayList entryids) // 0 - check last report, 1 - check each target
        {
            try
            {
                post = new PostSubmitter(cookie);
                post.Url = server+"build.php?gid=16&tt=99";
                post.PostItems.Add("action", "startRaid");
                post.PostItems.Add("direction", "asc");
                post.PostItems.Add("lid", lid.ToString());
                post.PostItems.Add("sort", "distance");
                post.PostItems.Add("a", a);
                post.Type = PostSubmitter.PostTypeEnum.Post;
                foreach (string entry in entryids)
                {
                    post.PostItems.Add("slot[" + entry + "]", "on");
                }
                string result = post.Post();
                Thread.Sleep(initTime + rand.Next(randomRange));
                if (debug)
                {
                    Util.writeStringToFile(result, "activatefarmlist2.html");
                }
                if (result.Contains("raids have been made"))
                {
                    Console.WriteLine("farmlist activated");
                    return 0;
                }
                else if (result.Contains("The account has been banned temporarily."))
                {
                    return -2;
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                if (ex.Message.Contains("The remote server returned an error: (500) Internal Server Error."))
                {
                    return 0;
                }
                return -1;
            }
        }

        public FarmList[] getFarmList()
        {
            return null;
        }

        //not fixed
        public Thread[] prepareMassAttack(int n, int[,] attacks, string x, string y)
        {
            try
            {
                PostSubmitter[] massPost = new PostSubmitter[n];
                Thread[] oThread = new Thread[n];
                for (int i = 0; i < n; i++)
                {
                    massPost[i] = new PostSubmitter(cookie);
                    String result = massPost[i].request(server + "build.php?id=39&tt=2");
                    if (debug)
                    {
                        Util.writeStringToFile(result, "sendtroop1.html");
                    }
                    Regex re = new Regex("<input type=\"hidden\" name=\"timestamp\" value=\"(\\d+)\" />");
                    MatchCollection matches = re.Matches(result);
                    String timestamp = matches[0].Groups[1].Value;
                    re = new Regex("<input type=\"hidden\" name=\"timestamp_checksum\"		value=\"(.+?)\" />");
                    matches = re.Matches(result);
                    String timestamp_checksum = matches[0].Groups[1].Value;
                    re = new Regex("<input type=\"hidden\" name=\"b\"		value=\"(.+?)\" />");
                    matches = re.Matches(result);
                    String b = matches[0].Groups[1].Value;
                    massPost[i] = new PostSubmitter(cookie);
                    massPost[i].Url = server + "build.php?id=39&tt=2";
                    massPost[i].PostItems.Add("timestamp", timestamp);
                    massPost[i].PostItems.Add("timestamp_checksum", timestamp_checksum);
                    massPost[i].PostItems.Add("b", b);
                    massPost[i].PostItems.Add("c", "3");
                    massPost[i].PostItems.Add("dname", "");
                    massPost[i].PostItems.Add("s1", "ok");
                    for (int j = 1; j <= 11; j++)
                    {
                        massPost[i].PostItems.Add("t" + j, attacks[i, j].ToString());
                    }
                    massPost[i].PostItems.Add("x", x.ToString());
                    massPost[i].PostItems.Add("y", y.ToString());
                    massPost[i].Type = PostSubmitter.PostTypeEnum.Post;
                    result = massPost[i].Post();
                    if (debug)
                    {
                        Util.writeStringToFile(result, "sendtroop2.html");
                    }
                    re = new Regex("name=\"timestamp\"\\svalue=\"(\\d+)\" />");
                    matches = re.Matches(result);
                    timestamp = matches[0].Groups[1].Value;
                    re = new Regex("value=\"(.+?)\" /> <input type=\"hidden\" name=\"id\"");
                    matches = re.Matches(result);
                    timestamp_checksum = matches[0].Groups[1].Value;

                    re = new Regex("name=\"id\"\\svalue=\"(\\d+)\" />");
                    matches = re.Matches(result);
                    String id = matches[0].Groups[1].Value;

                    re = new Regex("name=\"a\"\\svalue=\"(\\d+)\" />");
                    matches = re.Matches(result);
                    String a = matches[0].Groups[1].Value;

                    re = new Regex("name=\"kid\"\\svalue=\"(\\d+)\" />");
                    matches = re.Matches(result);
                    String kid = matches[0].Groups[1].Value;

                    massPost[i] = new PostSubmitter(cookie);
                    massPost[i].Url = server + "build.php?id=39&tt=2";
                    massPost[i].PostItems.Add("timestamp", timestamp);
                    massPost[i].PostItems.Add("timestamp_checksum", timestamp_checksum);
                    massPost[i].PostItems.Add("a", a);
                    massPost[i].PostItems.Add("c", "3");
                    massPost[i].PostItems.Add("id", id);
                    massPost[i].PostItems.Add("kid", kid);
                    massPost[i].PostItems.Add("s1", "ok");
                    massPost[i].PostItems.Add("kata", attacks[i, 12].ToString());
                    massPost[i].PostItems.Add("kata2", attacks[i, 13].ToString());
                    for (int j = 1; j <= 11; j++)
                    {
                        massPost[i].PostItems.Add("t" + j, attacks[i, j].ToString());
                    }
                    massPost[i].Type = PostSubmitter.PostTypeEnum.Post;
                    FastAttack fastattack = new FastAttack(massPost[i]);
                    oThread[i] = new Thread(new ThreadStart(fastattack.attack));

                }
                return oThread;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                return null;
            }
        }

        //not fixed
        public bool launchMassAttack(Thread[] oThread, int delay)
        {
            for (int i = 0; i < oThread.Length; i++)
            {
                oThread[i].Start();
                Thread.Sleep(delay);
            }
            return true;

        }

        //not fixed
        public bool sendRes(string fromId, string toName, int lumber, int clay, int iron, int crop)
        {
            post = new PostSubmitter(cookie);
            post.Url = server + "build.php?gid=16&tt=99";
            String result = post.request(server + "build.php?newdid=" + fromId + "&t=5&gid=17");
            Thread.Sleep(initTime + rand.Next(randomRange));
            if (debug)
            {
                Util.writeStringToFile(result, "sendres1.html");
            }
            Regex re = new Regex("<span id=\"merchantCapacityValue\">(\\d+)</span>");
            MatchCollection matches = re.Matches(result);
            try
            {
                int capacity = Convert.ToInt32(matches[0].Groups[1].Value);
                if (capacity == 0) return false;
                int total = lumber + clay + iron + crop;
                if (total > capacity)
                {
                    lumber = capacity * lumber / total;
                    clay = capacity * clay / total;
                    iron = capacity * iron / total;
                    crop = capacity * crop / total;
                }
                post = new PostSubmitter(cookie);
                post.Url = server + "ajax.php?cmd=prepareMarketplace";
                post.PostItems.Add("dname", toName);
                re = new Regex("id=\"id\" value=\"(\\d+)\"");
                matches = re.Matches(result);
                string id = matches[0].Groups[1].Value;
                post.PostItems.Add("id", id);
                post.PostItems.Add("r1", lumber.ToString());
                post.PostItems.Add("r2", clay.ToString());
                post.PostItems.Add("r3", iron.ToString());
                post.PostItems.Add("r4", crop.ToString());
                re = new Regex("id=\"t\"		value=\"(\\d+)\"");
                matches = re.Matches(result);
                string t = matches[0].Groups[1].Value;
                post.PostItems.Add("t", t);
                post.PostItems.Add("x", "");
                post.PostItems.Add("x2", "1");
                post.PostItems.Add("y", "");
                post.Type = PostSubmitter.PostTypeEnum.Post;
                result = post.Post();
                Thread.Sleep(initTime + rand.Next(randomRange));
                if (debug)
                {
                    Util.writeStringToFile(result, "sendres2.html");
                }
                post = new PostSubmitter(cookie);
                post.Url = server + "ajax.php?cmd=prepareMarketplace";
                re = new Regex("id=\\\\\"a\\\\\" value=\\\\\"(\\d+)\\\\\"");
                matches = re.Matches(result);
                string a = matches[0].Groups[1].Value;
                Console.WriteLine(a);
                re = new Regex("id=\\\\\"c\\\\\" value=\\\\\"(.+?)\\\\\"");
                matches = re.Matches(result);
                string c = matches[0].Groups[1].Value;
                Console.WriteLine(c);
                re = new Regex("id=\\\\\"id\\\\\" value=\\\\\"(\\d+)\\\\\"");
                matches = re.Matches(result);
                id = matches[0].Groups[1].Value;
                Console.WriteLine(id);
                re = new Regex("id=\\\\\"kid\\\\\" value=\\\\\"(\\d+)\\\\\"");
                matches = re.Matches(result);
                string kid = matches[0].Groups[1].Value;
                Console.WriteLine(kid);
                re = new Regex("id=\\\\\"sz\\\\\" value=\\\\\"(\\d+)\\\\\"");
                matches = re.Matches(result);
                string sz = matches[0].Groups[1].Value;
                Console.WriteLine(sz);
                post.PostItems.Add("a", a);
                post.PostItems.Add("c", c);
                post.PostItems.Add("id", id);
                post.PostItems.Add("kid", kid);
                post.PostItems.Add("r1", lumber.ToString());
                post.PostItems.Add("r2", clay.ToString());
                post.PostItems.Add("r3", iron.ToString());
                post.PostItems.Add("r4", crop.ToString());
                post.PostItems.Add("sz", sz);
                post.PostItems.Add("t", "5");
                post.PostItems.Add("x2", "1");
                post.Type = PostSubmitter.PostTypeEnum.Post;
                result = post.Post();
                Thread.Sleep(initTime + rand.Next(randomRange));
                if (debug)
                {
                    Util.writeStringToFile(result, "sendres3.html");
                }
                if (result.Contains("Resources have been dispatched."))
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
            return false;
        }

        public VillageInfo getHalfVillageInfo(String id)
        {
            VillageInfo info = new VillageInfo();
            String result = post.request(server + "dorf1.php?newdid=" + id);
            Thread.Sleep(initTime + rand.Next(randomRange));
            if (debug)
            {
                Util.writeStringToFile(result, "villageinfo.html");
            }
            ArrayList list = new ArrayList();
            Regex re = new Regex("<area href=\"(.+?)\" coords=\"(.+?)\" shape=\"(.+?)\" title=\"(.+?)\"");
            MatchCollection matches = re.Matches(result);
            for (int i = 0; i < matches.Count; i++)
            {
                Building res = new Building();
                res.buildLink = matches[i].Groups[1].Value;
                res.resField = true;
                Regex re1 = new Regex("(.+?) &lt;span class=&quot;level&quot;&gt;.+?\\s*(\\d+)&lt;");
                MatchCollection matches1 = re1.Matches(matches[i].Groups[4].Value);
                if (matches1.Count > 0)
                {
                    res.name = matches1[0].Groups[1].Value.Replace("&#039;", "'");
                    res.level = Convert.ToInt32(matches1[0].Groups[2].Value);
                    re1 = new Regex("&lt;span class=&quot;notice&quot;&gt;.+?(\\d+)&lt;/span&gt;");
                    matches1 = re1.Matches(matches[i].Groups[4].Value);
                    if (matches1.Count > 0)
                    {
                        res.level = Convert.ToInt32(matches1[0].Groups[1].Value);
                        Console.WriteLine("hehe " + res.level);
                    }
                    list.Add(res);
                }
            }
            list.Sort(new BuildingComparer());
            info.resFields = list;
            re = new Regex("<span id=\"l1\" class=\"value[ small]*\">(\\d+)</span>");
            matches = re.Matches(result);
            Console.WriteLine(matches[0].Groups[1].Value);
            info.lumber = Convert.ToInt32(matches[0].Groups[1].Value);

            re = new Regex("<span id=\"l2\" class=\"value[ small]*\">(\\d+)</span>");
            matches = re.Matches(result);
            info.clay = Convert.ToInt32(matches[0].Groups[1].Value);

            re = new Regex("<span id=\"l3\" class=\"value[ small]*\">(\\d+)</span>");
            matches = re.Matches(result);
            info.iron = Convert.ToInt32(matches[0].Groups[1].Value);

            re = new Regex("<span id=\"l4\" class=\"value[ small]*\">(\\d+)</span>");
            matches = re.Matches(result);
            info.crop = Convert.ToInt32(matches[0].Groups[1].Value);
            
            re = new Regex("<span id=\"stockBarFreeCrop\" class=\"value[ small]*\">(-?\\d+)</span>");
            matches = re.Matches(result);
            info.freeCop = Convert.ToInt32(matches[0].Groups[1].Value);

            re = new Regex("<span class=\"value[ small]*\" id=\"stockBarWarehouse\">(\\d+)</span>");
            matches = re.Matches(result);
            info.maxStore = Convert.ToInt32(matches[0].Groups[1].Value);

            re = new Regex("<span class=\"value[ small]*\" id=\"stockBarGranary\">(\\d+)</span>");
            matches = re.Matches(result);
            info.maxCrop = Convert.ToInt32(matches[0].Groups[1].Value);
            re = new Regex("<img src=\"img/x\\.gif\" class=\"del\" title=\".+?\" alt=\".+?\" />\\s*</a>\\s*<div class=\"name\">\\s*(.+?)\\s*<span class=\"lvl\">.+? (\\d+)</span>\\s*</div>\\s*<div class=\"buildDuration\">\\s*<span id=\"timer\\d+\">(.+?)</span>");
            matches = re.Matches(result);
            ArrayList inBuilds = new ArrayList();
            for (int i = 0; i < matches.Count; i++)
            {
                BuildingBuilding building = new BuildingBuilding();
                building.name = matches[i].Groups[1].Value;
                building.level = Convert.ToInt32(matches[i].Groups[2].Value);
                building.timeLeft = matches[i].Groups[3].Value;
                inBuilds.Add(building);
            }
            info.inBuilds = inBuilds;
            re = new Regex("coordinateX&quot;&gt;\\((-?\\d+)&lt;");
            matches = re.Matches(result);
            info.x = Convert.ToInt32(matches[0].Groups[1].Value);

            re = new Regex("coordinateY&quot;&gt;(-?\\d+)\\)&lt;");
            matches = re.Matches(result);
            info.y = Convert.ToInt32(matches[0].Groups[1].Value);

            Hashtable troops = new Hashtable();
            re = new Regex("<img class=\"unit u\\d+\" src=\"img/x\\.gif\" alt=\"(.+?)\" title=\".+?\" /></a></td>\\s*<td class=\"num\">(\\d+)</td>");
            matches = re.Matches(result);
            if (matches.Count > 0)
            {
                for (int i = 0; i < matches.Count; i++)
                {
                    troops.Add(matches[i].Groups[1].Value, Convert.ToInt32(matches[i].Groups[2].Value));
                }
            }
            info.troops = troops;

            info.waves = 0;
            info.taskComplete = 0;
            return info;
        }

        public VillageInfo getVillageInfo(String id)
        {
            VillageInfo info = getHalfVillageInfo(id);
            String result = post.request(server + "dorf2.php?newdid=" + id);
            Thread.Sleep(initTime + rand.Next(randomRange));
            if (debug)
            {
                Util.writeStringToFile(result, "villageinfo2.html");
            }
            Regex re = new Regex("<area alt=\".+?\" title=\"(.+?)\" shape=\"poly\" coords=\".+?\" href=\"(.+?)\" />");
            MatchCollection matches = re.Matches(result);
            ArrayList buildingList = new ArrayList();
            ArrayList emptySlots = new ArrayList();
            for (int i = 0; i < matches.Count; i++)
            {
                String title = matches[i].Groups[1].Value;
                String buildLink = matches[i].Groups[2].Value;
                Regex re1 = new Regex("(.+?) &lt;span class=&quot;level&quot;&gt;.+? (\\d+)");
                MatchCollection matches1 = re1.Matches(title);
                if (matches1.Count > 0)
                {
                    Building building = new Building();
                    building.buildLink = buildLink;
                    building.name = matches1[0].Groups[1].Value.Replace("&#039;", "'");
                    building.level = Convert.ToInt32(matches1[0].Groups[2].Value);
                    building.resField = false;
                    re1 = new Regex("&lt;span class=&quot;notice&quot;&gt;.+? (\\d+)&lt;/span&gt;");
                    matches1 = re1.Matches(matches[i].Groups[1].Value);
                    if (matches1.Count > 0)
                    {
                        building.level = Convert.ToInt32(matches1[0].Groups[1].Value);
                    }
                    buildingList.Add(building);
                }
                else
                {   //Empty slot
                    Building emptySlot = new Building();
                    emptySlot.name = title;
                    emptySlot.level = 0;
                    emptySlot.resField = false;
                    emptySlot.buildLink = buildLink;
                    emptySlots.Add(emptySlot);
                }


                
            }
            info.buildings = buildingList;
            info.emptySlots = emptySlots;
            ArrayList buildable = new ArrayList();
            if (emptySlots.Count > 0)
            {
                Building emptySlot = (Building)emptySlots[0];
                for (int i = 1; i <= 3; i++)
                {
                    result = post.request(server + emptySlot.buildLink + "&category="+i+"&newdid=" + id);
                    if (debug)
                    {
                        Util.writeStringToFile(result, "villageinfo3.html");
                    }
                    re = new Regex("<h2>(.+?)</h2>\\s*<div class=\"build_desc\">\\s*<a href=\"#\" onclick=\"return Travian\\.Game\\.iPopup\\((\\d+),(\\d+)\\);\"");
                    matches = re.Matches(result);
                    for (int j = 0; j < matches.Count; j++)
                    {
                        Building building = new Building();
                        building.name = matches[j].Groups[1].Value;
                        building.level = 0;
                        building.resField = false;
                        building.buildLink = matches[j].Groups[2].Value;
                        buildable.Add(building);
                    }
                }
            }
            info.buildable = buildable;
            return info;
        }

        public int getMaxCranny(string id)
        {
            try
            {
                String result = post.request(server+"build.php?id=" + id);
                Thread.Sleep(initTime + rand.Next(randomRange));
                if (debug)
                {
                    Util.writeStringToFile(result, "cranny.html");
                }
                Regex re = new Regex("Units per resource hidden by all crannies:</th>		<td><span class=\"number\">(\\d+)</span>");
                MatchCollection matches = re.Matches(result);
                return Convert.ToInt32(matches[0].Groups[1].Value);
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public bool build(String buildLink1, int buildingID)
        {
            String result = post.request(server + buildLink1);
            Thread.Sleep(initTime + rand.Next(randomRange));
            if (debug)
            {
                Util.writeStringToFile(result, "buildres1.html");
            } 
            Regex re = new Regex("<button  type=\"button\" value=\".+?\" id=\".+?\" class=\"green build\" onclick=\"window.location.href = '(.+?)'; return false;\">");
            MatchCollection matches = re.Matches(result);
            String buildLink = "";
            if (matches.Count > 0)
            {
                buildLink = matches[0].Groups[1].Value;
                buildLink = buildLink.Replace("&amp;", "&");
            }
            else
            {
                if (result.Contains("&amp;category=1")) //Test for Empty slot
                {
                    re = new Regex("<button  type=\"button\" value=\".+?\" id=\".+?\" class=\"green new\" onclick=\"window\\.location\\.href = 'dorf2\\.php\\?a=" + buildingID + "&amp;(.+?)'");
                    matches = re.Matches(result);
                    if (matches.Count > 0)
                    {
                        buildLink = matches[0].Groups[1].Value.Replace("&amp;", "&");
                        buildLink = "dorf2.php?a=" + buildingID + "&" + buildLink;
                    }
                    else// Test for another two tabs
                    {
                        for (int category = 2; category <= 3; category++)
                        {
                            result = post.request(server + buildLink1 + "&category=" + category);
                            Thread.Sleep(initTime + rand.Next(randomRange));
                            if (debug)
                            {
                                Util.writeStringToFile(result, "buildres1.html");
                            }
                            re = new Regex("<button  type=\"button\" value=\".+?\" id=\".+?\" class=\"green new\" onclick=\"window\\.location\\.href = 'dorf2\\.php\\?a=" + buildingID + "&amp;(.+?)'");
                            matches = re.Matches(result);
                            if (matches.Count > 0)
                            {
                                buildLink = matches[0].Groups[1].Value.Replace("&amp;", "&");
                                buildLink = "dorf2.php?a=" + buildingID + "&" + buildLink;
                                break;
                            }
                        }
                    }
                }
            }
            if (!buildLink.Equals(""))
            {
                Console.WriteLine(server + buildLink);
                result = post.request(server + buildLink);
                Thread.Sleep(initTime + rand.Next(randomRange));
                if (debug)
                {
                    Util.writeStringToFile(result, "buildres2.html");
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool addFighting(int number)
        {
            post = new PostSubmitter(cookie);
            post.Url = server + "ajax.php?cmd=heroSetAttributes";
            post.PostItems.Add("cmd", "heroSetAttributes");
            post.PostItems.Add("resource", "0");
            post.PostItems.Add("attackBehaviour", "hide");
            post.PostItems.Add("attributes[power]", number.ToString());
            post.PostItems.Add("attributes[offBonus]", "0");
            post.PostItems.Add("attributes[defBonus]", "0");
            post.PostItems.Add("attributes[productionPoints]", "0");
            post.Type = PostSubmitter.PostTypeEnum.Post;
            string result = post.Post();
            if (debug)
            {
                Util.writeStringToFile(result, "afterlogin.html");
            }
            if (result.Contains("<a class=\" active\" href=\"dorf1.php\" accesskey=\"1\" title=\"Resources\"></a>"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public Task checkTask()
        {
            post = new PostSubmitter(cookie);
            String result = post.request(server + "ajax.php?cmd=qst");
            if (debug)
            {
                Util.writeStringToFile(result, "quest.html");
            }
            Regex re = new Regex("Task (\\d+)");
            MatchCollection matches = re.Matches(result);
            int number = 0;
            if (matches.Count > 0)
            {
                number = Convert.ToInt32(matches[0].Groups[1].Value);
            }
            else
            {
                return new Task(0, false, "");
            }
            if (result.Contains("Take your reward and continue with the next task.") || result.Contains("Continue with the next task."))
            {
                return new Task(number, true, "");
            }
            else
            {
                re = new Regex("of <b>(.+?)<\\\\/b>");
                matches = re.Matches(result);
                if (matches.Count > 0)
                {
                    return new Task(number, false, matches[0].Groups[1].Value);
                }
                else if (number == 21)
                {
                    if (result.Contains("Construct a granary"))
                    {
                        return new Task(number, false, "Construct a granary");
                    }
                }
                return new Task(number, false, "");
            }
        }

        public bool completeQuest(int taskNumber)
        {
            post = new PostSubmitter(cookie);
            String result = post.request(server + "dorf1.php?a="+taskNumber+"&cmd=qst&questUpdate=1&questNext=1");
            Thread.Sleep(initTime + rand.Next(randomRange));
            return true;
        }

        public bool completeLaterQuest(int taskNumber)
        {
            post = new PostSubmitter(cookie);
            String result = post.request(server + "dorf1.php?questUpdate=1&cmd=qst&questId=-"+taskNumber);
            Thread.Sleep(initTime + rand.Next(randomRange));
            return true;
        }

        public int getRank()
        {
            post = new PostSubmitter(cookie);
            String result = post.request(server + "statistiken.php");
            Thread.Sleep(initTime + rand.Next(randomRange));
            if (debug)
            {
                Util.writeStringToFile(result, "statistiken.html");
            }
            Regex re = new Regex("<tr class=\"hl\" >   <td class=\"ra  fc\" >(\\d+)\\.</td>");
            MatchCollection matches = re.Matches(result);
            if (matches.Count > 0)
            {
                return Convert.ToInt32(matches[0].Groups[1].Value);
            }
            else
            {
                return 0;
            }
        }

        public bool readTaskMessage()
        {
            post = new PostSubmitter(cookie);
            String result = post.request(server + "nachrichten.php");
            Thread.Sleep(initTime + rand.Next(randomRange));
            if (debug)
            {
                Util.writeStringToFile(result, "nachrichten.html");
            }
            Regex re = new Regex("<a href=\"nachrichten.php\\?id=(\\d+)\">										Message from the Taskmaster");
            MatchCollection matches = re.Matches(result);
            if (matches.Count > 0)
            {
                post = new PostSubmitter(cookie);
                post.request(server + "nachrichten.php?id=" + matches[0].Groups[1].Value);
                Thread.Sleep(initTime + rand.Next(randomRange));
                return true;
            }
            return false;
        }

        public int buyTroop(string villageId, int gid, int troopType, int number, int maxTimeInQueueInSeconds)
        {
            post = new PostSubmitter(cookie);
            String result = post.request(server + "build.php?gid="+gid+"&newdid=" + villageId);
            Thread.Sleep(initTime + rand.Next(randomRange));
            if (debug)
            {
                Util.writeStringToFile(result, "buytroop1.html");
            }
            Regex re = new Regex("<td class=\"dur\"><span id=\"timer(\\d+)\">(.+?)</span></td>");
            MatchCollection matches = re.Matches(result);
            string timeInQueue = "0:0:0";
            if (matches.Count > 0)
            {
                timeInQueue = matches[matches.Count-1].Groups[2].Value;
            }
            Console.WriteLine("time in queue: " + timeInQueue);
            int seconds = Util.timeToSeconds(timeInQueue);
            re = new Regex("\\(present \\d+\\).+?<span class=\"clocks\" title=\"duration\"><img class=\"clock\" src=\"img/x\\.gif\" alt=\"duration\" />([0-9:]+?)</span>(<button([^:]+?)/></button>)?<div class=\"clear\"></div></div>						<span class=\"value\">Amount</span>						<input type=\"text\" class=\"text\" name=\"t" + troopType + "\" value=\"0\" maxlength=\"4\" /><span class=\"value\"> / </span>						<a href=\"\\#\" onclick=\"\\$\\(this\\)\\.getParent\\('div\\.details'\\)\\.getElement\\('input'\\)\\.value=(\\d+); return false;\">(\\d+)</a>");
            matches = re.Matches(result);
            Console.WriteLine("time train 1 troop: " +matches[0].Groups[1].Value);
            int timeTrain1Troop = Util.timeToSeconds(matches[0].Groups[1].Value);
            Console.WriteLine("max train by res: " +matches[0].Groups[4].Value);
            int maxTroopByRes = Convert.ToInt32(matches[0].Groups[4].Value);
            if (seconds < maxTimeInQueueInSeconds && maxTroopByRes > 0)
            {
                int troopToTrain = (maxTimeInQueueInSeconds - seconds) / timeTrain1Troop;
                troopToTrain = Math.Min(Math.Min(troopToTrain, number), maxTroopByRes);
                re = new Regex("<input type=\"hidden\" name=\"a\" value=\"(\\d+)\" />");
                matches = re.Matches(result);
                string a = matches[0].Groups[1].Value;
                re = new Regex("<input type=\"hidden\" name=\"id\" value=\"(\\d+)\" />");
                matches = re.Matches(result);
                string id = matches[0].Groups[1].Value;
                re = new Regex("<input type=\"hidden\" name=\"s\" value=\"(\\d+)\" />");
                matches = re.Matches(result);
                string s = matches[0].Groups[1].Value;
                re = new Regex("<input type=\"hidden\" name=\"z\" value=\"(.+?)\" />");
                matches = re.Matches(result);
                string z = matches[0].Groups[1].Value;
                post = new PostSubmitter(cookie);
                post.Url = server + "build.php";
                post.PostItems.Add("a", a);
                post.PostItems.Add("id", id);
                post.PostItems.Add("s", s);
                post.PostItems.Add("s1", "ok");

                post.PostItems.Add("t" + troopType, troopToTrain.ToString());
                post.PostItems.Add("z", z);
                post.Type = PostSubmitter.PostTypeEnum.Post;
                result = post.Post();
                Thread.Sleep(initTime + rand.Next(randomRange));
                Console.WriteLine("buy "+troopToTrain+" type"+troopType);
                if (debug)
                {
                    Util.writeStringToFile(result, "buytroop2.html");
                }
                return troopToTrain;
            }
            else
            {
                return 0;
            }
        }

        public bool party(string villageId)
        {
            post = new PostSubmitter(cookie);
            String result = post.request(server + "build.php?gid=24&newdid=" + villageId);
            Thread.Sleep(initTime + rand.Next(randomRange));
            if (debug)
            {
                Util.writeStringToFile(result, "party1.html");
            }
            Regex re = new Regex("<button type=\"button\" value=\"hold\" title=\"hold\" onclick=\"window\\.location\\.href = '(.+?)'; return false;\">");
            MatchCollection matches = re.Matches(result);
            if (matches.Count > 1)
            {
                String partyLink = matches[1].Groups[1].Value;
                post.request(server + partyLink);
                Thread.Sleep(initTime + rand.Next(randomRange));
                return true;
            }
            else if (matches.Count > 0)
            {
                String partyLink = matches[0].Groups[1].Value;
                post.request(server + partyLink);
                Thread.Sleep(initTime + rand.Next(randomRange));
                return true;
            }
            else
            {
                return false;
            }
        }

        public int getRaidStatistics()
        {
            post = new PostSubmitter(cookie);
            String result = post.request(server + "statistiken.php?id=0&idSub=3");
            Thread.Sleep(initTime + rand.Next(randomRange));
            if (debug)
            {
                Util.writeStringToFile(result, "raidstatistics.html");
            }
            Regex re = new Regex("<td class=\".+?\">(\\d+)</td>");
            MatchCollection matches = re.Matches(result);
            int i = matches.Count;
            return Convert.ToInt32(matches[i-1].Groups[1].Value);
        }

        public int getPopulation()
        {
            post = new PostSubmitter(cookie);
            String result = post.request(server + "statistiken.php");
            Thread.Sleep(initTime + rand.Next(randomRange));
            if (debug)
            {
                Util.writeStringToFile(result, "pop.html");
            }
            Regex re = new Regex("<tr class=\"hl\" >.+?<td class=\"pop \" >(\\d+)</td>.+?</tr>");
            MatchCollection matches = re.Matches(result);
            return Convert.ToInt32(matches[0].Groups[1].Value);
        }

        public bool postRankInfo(int rank)
        {
            post = new PostSubmitter(cookie);
            post.Url = server + "ajax.php?cmd=qst&cr=LTGA";
            post.PostItems.Add("val", rank.ToString());
            post.Type = PostSubmitter.PostTypeEnum.Post;
            string result = post.Post();
            return true;
        }

        public bool postNeiborInfo(string x, string y)
        {
            post = new PostSubmitter(cookie);
            post.Url = server + "ajax.php?cmd=qst&cr=TLGN";
            post.PostItems.Add("x", x.ToString());
            post.PostItems.Add("y", y.ToString());
            post.Type = PostSubmitter.PostTypeEnum.Post;
            string result = post.Post();
            return true;
        }

        public bool readAllMiscReport()
        {
            post = new PostSubmitter(cookie);
            String result = post.request(server + "berichte.php?t=3");
            Thread.Sleep(initTime + rand.Next(randomRange));
            if (debug)
            {
                Util.writeStringToFile(result, "miscreports.html");
            }
            Regex re = new Regex("<a class=\"adventure\" href=\"(.+?)\">");
            MatchCollection matches = re.Matches(result);
            for (int i = 0; i < matches.Count; i++)
            {
                string link = matches[i].Groups[1].Value.Replace("&amp;", "&");
                post.request(server+link);
                Thread.Sleep(initTime + rand.Next(randomRange));
            }
            return true;
        }

        public bool setDoveOfPeace()
        {
            post = new PostSubmitter(cookie);
            String result = post.request(server+"spieler.php?s=1");
            Thread.Sleep(initTime + rand.Next(randomRange));
            if (debug)
            {
                Util.writeStringToFile(result, "profile.html");
            }
            Regex re = new Regex("name=\"did\" value=\"(\\d+)\"");
            MatchCollection matches = re.Matches(result);
            string did = matches[0].Groups[1].Value;
             re = new Regex("name=\"e\" value=\"(\\d+)\"");
             matches = re.Matches(result);
             string e = matches[0].Groups[1].Value;
            post.Url = server + "spieler.php";
            post.PostItems.Add("s1", "save");
            post.PostItems.Add("be1", "[#0]");
            post.PostItems.Add("be2", "");
            post.PostItems.Add("jahr", "");
            post.PostItems.Add("monat", "0");
            post.PostItems.Add("mw", "0");
            post.PostItems.Add("ort", "");
            post.PostItems.Add("tag", "");
            post.PostItems.Add("did", did);
            post.PostItems.Add("e", e);
            Random rnd = new Random();
            post.PostItems.Add("dname["+did+"]=", rnd.Next().ToString());
            post.Type = PostSubmitter.PostTypeEnum.Post;
            post.Post();
            Thread.Sleep(initTime + rand.Next(randomRange));
            return true;
        }

        public bool postBarrackCost()
        {
            post = new PostSubmitter(cookie);
            post.Url = server + "ajax.php?cmd=qst&cr=pkQk";
            post.PostItems.Add("val", "210");
            post.Type = PostSubmitter.PostTypeEnum.Post;
            string result = post.Post();
            return true;
        }

        public bool postGold50()
        {
            post = new PostSubmitter(cookie);
            post.Url = server + "ajax.php?cmd=qst&cr=piea";
            post.PostItems.Add("val", "50");
            post.Type = PostSubmitter.PostTypeEnum.Post;
            string result = post.Post();
            return true;
        }

        public bool choseEconomy()
        {
            post = new PostSubmitter(cookie);
            post.Url = server + "ajax.php?cmd=qst&cr=eZUe";
            post.PostItems.Add("val", "1");
            post.Type = PostSubmitter.PostTypeEnum.Post;
            string result = post.Post();
            return true;
        }

        public bool autoVillage2()
        {
            return true;
        }

        public void autoBid(string link, int price)
        {
            int silver = 99999;
            while (silver > price)
            {
                post = new PostSubmitter(cookie);
                String result = post.request(link);
                Thread.Sleep(initTime + rand.Next(randomRange));
                if (debug)
                {
                    Util.writeStringToFile(result, "autobid.html");
                }
                Regex re = new Regex("<td  class=\"silver\" title=\"[^<]+?\">			(\\d+)			</td>			<td class=\"time\"><span id=\"timer\\d+\">[0-9:]+?</span>						</td>							<td class=\"bid\">																		<a class=\"bidButton openedClosedSwitch switchClosed\" href=\"(.+?)\">bid</a>");
                MatchCollection matches = re.Matches(result);
                if (matches.Count == 0) return;
                bool bidded = false;
                for (int i = 0; i < matches.Count; i++)
                {
                    int currentPrice = Convert.ToInt32(matches[i].Groups[1].Value);
                    string bidLink = matches[i].Groups[2].Value.Replace("&amp;", "&");
                    if (currentPrice < price)
                    {
                        silver = bid(bidLink, price);
                        Console.WriteLine(silver);
                        bidded = true;
                        break;
                    }
                }
                if (!bidded) return;
            }
        }

        public int bid(string link, int price)
        {
            try
            {
                post = new PostSubmitter(cookie);
                String result = post.request(server + link);
                Thread.Sleep(initTime + rand.Next(randomRange));
                if (debug)
                {
                    Util.writeStringToFile(result, "bid.html");
                }
                post = new PostSubmitter(cookie);
                post.Url = server + "hero_auction.php";
                Regex re = new Regex("<input type=\"hidden\" name=\"a\" value=\"(\\d+)\" />");
                MatchCollection matches = re.Matches(result);
                string a = matches[0].Groups[1].Value;
                Console.WriteLine(a);
                re = new Regex("<input type=\"hidden\" name=\"filter\" value=\"(\\d+)\" />");
                matches = re.Matches(result);
                string filter = matches[0].Groups[1].Value;
                Console.WriteLine(filter);
                re = new Regex("<input type=\"hidden\" name=\"page\" value=\"(\\d+)\" />");
                matches = re.Matches(result);
                string page = matches[0].Groups[1].Value;
                Console.WriteLine(page);
                re = new Regex("<input type=\"hidden\" name=\"z\" value=\"(.+?)\" />");
                matches = re.Matches(result);
                string z = matches[0].Groups[1].Value;
                Console.WriteLine(z);
                post.PostItems.Add("a", a);
                post.PostItems.Add("action", "buy");
                post.PostItems.Add("filter", filter);
                post.PostItems.Add("maxBid", price.ToString());
                post.PostItems.Add("page", page);
                post.PostItems.Add("z", z);
                post.Type = PostSubmitter.PostTypeEnum.Post;
                result = post.Post();
                Thread.Sleep(initTime + rand.Next(randomRange));
                if (debug)
                {
                    Util.writeStringToFile(result, "bid2.html");
                }
                re = new Regex("<img src=\"img/x\\.gif\" alt=\"Silver\" class=\"silver\" /><br />(\\d+)</a>");
                matches = re.Matches(result);
                return Convert.ToInt32(matches[0].Groups[1].Value);
            }
            catch (Exception ex)
            {
                return 99999;
            }
        }

        public void equip(string name)
        {
            HeroInfo info = getHeroInfo();
            if (!info.available) return;
            foreach (Item item in info.unequippedItems)
            {
                if (item.name.Equals(name))
                {
                    post = new PostSubmitter(cookie);
                    post.Url = server + "ajax.php?cmd=heroInventory";
                    post.PostItems.Add("a", info.a.ToString());
                    post.PostItems.Add("amount", "1");
                    post.PostItems.Add("c", info.c);
                    post.PostItems.Add("drid", item.slot);
                    post.PostItems.Add("id", item.id);
                    post.Type = PostSubmitter.PostTypeEnum.Post;
                    string result = post.Post();
                    Thread.Sleep(initTime + rand.Next(randomRange));
                    if (debug)
                    {
                        Util.writeStringToFile(result, "afterquip.txt");
                    }
                    break;
                }
            }
        }
    }
}
